﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Upreader.Usenet.Nntp
{
    public class InputLoggingStream : StreamWrapper
    {
        #region Constructor

        public InputLoggingStream(Stream baseStream, 
            TextWriter logWriter,
            Encoding encoding)
            : base(baseStream)
        {
            if (logWriter == null)
                throw new ArgumentNullException("logWriter");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            LogWriter = logWriter;
            Encoding = encoding;
        }

        #endregion // Constructor

        #region Properties

        public TextWriter LogWriter { get; private set; }

        public Encoding Encoding { get; private set; }

        #endregion // Properties

        #region Methods

        public override void Write(byte[] buffer, int offset, int count)
        {
            string logString = Encoding.GetString(buffer, offset, count);
            LogWriter.Write(logString);

            BaseStream.Write(buffer, offset, count);
        }

        #endregion // Methods
    }
}
